﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STS.Schema.Interfaces;
using STS.Schema.Collections;
using STS.Schema.Base;
using STS.Interfaces;
using STS.Schema.Enums;

namespace STS.Schema.MSSql
{
    public class MSSqlForeignKeyColumn : SqlObjectBase, IForeignKeyColumn
    {
        public override SqlObjectType ObjectType
        {
            get { return SqlObjectType.ForeignKeyColumn; }
        }

        [SqlQueryProperty("parent_object_id")]
        public override int? ParentObjectID { get; internal set; }

        [SqlQueryProperty("fk_table_name")]
        public string TableName { get; internal set; }

        [SqlQueryProperty("fk_table_schema")]
        public string TableSchema { get; internal set; }

        [SqlQueryProperty("fk_col_name")]
        public string ColumnName { get; internal set; }

        [SqlQueryProperty("pk_table_name")]
        public string ReferencedTableName { get; internal set; }

        [SqlQueryProperty("pk_table_schema")]
        public string ReferencedTableSchemaName { get; internal set; }

        [SqlQueryProperty("pk_col_name")]
        public string ReferencedColumnName { get; internal set; }

        public MSSqlForeignKeyColumn()
            : this(null, null)
        {

        }

        public MSSqlForeignKeyColumn(ISchemaFactory schemaFactory, IDatabase database)
            : base(schemaFactory, database)
        {

        }

        public override ISqlObjectCollection GetChildren<TChild>()
        {
            return base.Factory.GetObjectList<TChild>(this, null, base.Database);
        }

        public override string ToString()
        {
            return string.Format("[{0}].[{1}].{2} -> [{3}].[{4}].{5}", 
                this.TableSchema,
                this.TableName,
                this.ColumnName,
                this.ReferencedTableSchemaName,
                this.ReferencedTableName,
                this.ReferencedColumnName);
        }

    }
}
